﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using Utility;
using System.IO;
namespace WebTeamplate
{
    public partial class ForgotPass : BasePage
    {
        
        #region Fields and variable

        #endregion  Fields

        #region view state

        #endregion  view state

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RequiredFieldValidator refvUserName = (RequiredFieldValidator)PasswordRecovery1.UserNameTemplateContainer.FindControl("valRequireUserName");
                if (refvUserName.IsValid == false)
                    PasswordRecovery1.UserNameFailureText = string.Empty;
            }
            //check login
            if (Page.User.Identity.IsAuthenticated)//alredy login
            {
                FormsAuthentication.SignOut();
                Session.Abandon();
                Response.Redirect(this.GetCurrentPageUrl());
            }
            LoadDefaultData();

        }

        private void LoadDefaultData()
        {
            Button lbtnSend = (Button)PasswordRecovery1.UserNameTemplateContainer.FindControl("lbtnSend");
            lbtnSend.Text = Resources.Resource.LABEL_BUTTON_SEND;
            ClientDefaultButton.SetPageDefaultButton(this.Page, lbtnSend.ClientID);
            //LinkButton lbtnClose = (LinkButton)PasswordRecovery1.UserNameTemplateContainer.FindControl("btnClose");
            string script = @" <script language='javascript' type='text/javascript'>DisableParentForm('ctl00_phdMainContent_PasswordRecovery1_UserNameContainerID_btnClose')</script>";
            this.Page.RegisterStartupScript("DisableParentRecoveryPassForm", script);

            // lblRecoveryPassTitle.Text = LanguageManager.GetOutSizeText("lblRecoveryPassTitle");
            //lblRecoveryPassIntro.Text = LanguageManager.GetOutSizeText("lblRecoveryPassIntro");
            Label lbl = null;// (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("lblRecoveryPassStep1");
            //lbl.Text = LanguageManager.GetOutSizeText("lblRecoveryPassStep1");
            lbl = (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("lblUsername");
            RequiredFieldValidator refvUserName = (RequiredFieldValidator)PasswordRecovery1.UserNameTemplateContainer.FindControl("valRequireUserName");
            lbl.Text = Resources.Resource.LABEL_USERNAME;
            refvUserName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lbl));
            lbtnSend.ValidationGroup = refvUserName.ValidationGroup="pass";
            lbl = (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("lblTitlePopUp");
            //lbl.Text = "Thông báo";
            lbl = (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("lblClosePopupButton");
            // lbl.Text = "Đóng";

            lbl = (Label)PasswordRecovery1.SuccessTemplateContainer.FindControl("lblSuccess");
            lbl.Text =Resources.Resource.MESSAGE_FORGOTPASS_SUCESSFULL;

            // lbl = (Label)PasswordRecovery1.SuccessTemplateContainer.FindControl("lblSuccess");
            //  lbl.Text = LanguageManager.GetOutSizeText("lblRecoverySuccess");

            PasswordRecovery1.UserNameFailureText = "Send error";
         //   PasswordRecovery1.MailDefinition.From = "ngvanty@kng-vn.com";

        }

        #endregion

        #region Methods


        #endregion

        #region Events


        protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
        {
            e.Cancel = true;
            SendMail();
        }

        private void SendMail()
        {
            string mailBody = string.Empty;
            string title = string.Empty;
            TextBox txtUserName = (TextBox)PasswordRecovery1.UserNameTemplateContainer.FindControl("UserName");
            MembershipUser user = Membership.GetUser(txtUserName.Text);
            if (user != null)
            {
                GetMailTemplate(ref mailBody, ref title, user);
                if (mailBody != string.Empty)
                {
                    bool result=BaseFunctions.SendMail(CommonFunction.DEFAULT_MAILFROM, user.Email, title, mailBody, CommonFunction.DEFAULT_MAIL_SERVER_NAME, CommonFunction.DEFAULT_MAIL_SERVER_PORT);
                    Label lbl = (Label)PasswordRecovery1.SuccessTemplateContainer.FindControl("lblSuccess");
                    if(result)
                    lbl.Text = Resources.Resource.MESSAGE_FORGOTPASS_SUCESSFULL;
                    else lbl.Text = Resources.Resource.MESSAGE_SEND_MAIL_ERROR;

                }

            }
        }
        private void GetMailTemplate(ref string body, ref string title, MembershipUser user)
        {
            string filePath = Server.MapPath(CommonFunction.DEFAULT_TEMPLATE_MAIL_RECOVERYPASS);
            if (File.Exists(filePath))
            {
                string[] text = File.ReadAllLines(filePath);
                title = text.GetValue(0).ToString();
                for (int i = 1; i < text.Length; i++)
                {
                    body += text.GetValue(i).ToString() + "\r\n";
                }
                body = string.Format(body, user.UserName, user.GetPassword());
            }
        }
        #endregion
        protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
        {

        }
        protected void PasswordRecovery1_UserLookupError(object sender, EventArgs e)
        {
            //show message cannot access
            Label lbl = (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("lblError");
            lbl.Text = Resources.Resource.MESSAGE_FORGOTPASS_ERROR;
        }
        protected void ShowMessPanel(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

        }

    }
}